package io.jsen.meta.diff.context;

import com.google.common.collect.Maps;
import io.jsen.meta.diff.field.MetaFieldDefine;

import java.util.Map;

/**
 * 注册字段的delete值，可以是字段，可以是类型
 * <p>
 * 字段代表这个字段的值如果是这个则认为是删除
 * <p>
 * 类代表如果这个字段的类型是这个值则认为是删除
 * <p>
 * 优先获取字段的，如果没有字段的取类的
 *
 * @author jsen
 * @date 2021/4/6
 */
class MetaDeleteContext {

    private static final Map<MetaFieldDefine, Object> FIELD_DELETE_MAP = Maps.newHashMap();
    private static final Map<MetaFieldDefine, Object> TYPE_DELETE_MAP = Maps.newHashMap();

    static boolean isDelete(MetaFieldDefine field, Object val) {
        if (val == null) {
            return false;
        }
        if (field != null && FIELD_DELETE_MAP.containsKey(field)) {
            return FIELD_DELETE_MAP.get(field).equals(val);
        }
        if (TYPE_DELETE_MAP.containsKey(field)) {
            return TYPE_DELETE_MAP.get(field).equals(val);
        }
        return false;
    }

    static Object getDeleteVal(MetaFieldDefine field) {
        if (FIELD_DELETE_MAP.containsKey(field)) {
            return FIELD_DELETE_MAP.get(field);
        }
        return TYPE_DELETE_MAP.get(field);
    }

    static void registerFieldDelete(MetaFieldDefine field, Object val) {
        FIELD_DELETE_MAP.put(field, val);
    }

    static void registerTypeDelete(MetaFieldDefine clazz, Object val) {
        TYPE_DELETE_MAP.put(clazz, val);
    }

    private MetaDeleteContext() {
    }
}
